<?php
include_once './baseSQL.php';

class StuSql extends BaseSql
{


    function insert($stu)
    {

        try {
            $dbh = new PDO($this->dsn, $this->user, $this->pass); //初始化一个PDO对象

            $sql = "SELECT * FROM zyh_student WHERE stu_id like '" . $stu['id'] . "'";
            $stmt = $dbh->query($sql, PDO::FETCH_ASSOC);
            if ($stmt->rowCount() == 0) {
                $sql = "INSERT INTO zyh_student (`stu_id`, `stu_pwd`, `stu_name`, `stu_class`, `stu_college`) VALUE('" . $stu['id'] . "','" . $stu['id'] . "','" . $stu['name'] . "','" . $stu['class'] . "','" . $stu['college'] . "')";
                $dbh->exec($sql);
            }


            $dbh = null;
            return true;
        } catch (PDOException $e) {
            die("Error!: " . $e->getMessage() . "<br/>");
        }
    }

    function add($arr)
    {

        try {
            $dbh = new PDO($this->dsn, $this->user, $this->pass); //初始化一个PDO对象

            $stu_class = array_key_exists('stu_class', $arr) ? $arr['stu_class'] : '';
            $stu_college = array_key_exists('stu_college', $arr) ? $arr['stu_college'] : '';

            if (array_key_exists('isAdd', $arr) && $arr['isAdd']) {                 //添加一条记录
                $sql = "SELECT * FROM zyh_student WHERE stu_id like '" . $arr['stu_id'] . "'";
                $stmt = $dbh->query($sql, PDO::FETCH_ASSOC);
                $isExsit = $stmt->rowCount();         //查询是否已存在
                if ($isExsit == 0) {
                    $sql = "INSERT INTO zyh_student (`stu_id`, `stu_pwd`, `stu_name`, `stu_class`, `stu_college`) VALUE('" . $arr['stu_id'] . "','" . $arr['stu_id'] . "','" . $arr['stu_name'] . "','" . $stu_class . "','" . $stu_college . "')";
                    $count = $dbh->exec($sql);
                    if ($count > 0) {
                        $result = [
                            'code' => '0',
                            'msg' => '插入信息成功',
                        ];
                    } else {
                        $result = [
                            'code' => '1',
                            'msg' => '插入信息失败',
                        ];
                    }
                } else {
                    $result = [
                        'code' => '1',
                        'msg' => '学号已存在',
                    ];
                }
            } else {
                $sql = "UPDATE zyh_student SET `stu_name` = '" . $arr['stu_name'] . "', `stu_class` = '" . $stu_class . "', `stu_college` = '" .  $stu_college  . "' WHERE stu_id = '" . $arr['stu_id'] . "'";
                $count = $dbh->exec($sql);
                if ($count == 0) {
                    $result = [
                        'code' => '1',
                        'msg' => '修改信息失败',
                    ];
                } else {
                    $result = [
                        'code' => '0',
                        'msg' => '修改信息成功',
                    ];
                }
            }
            $dbh = null;

            //返回json
            echo json_encode($result);
        } catch (PDOException $e) {
            die("Error!: " . $e->getMessage() . "<br/>");
        }
    }


    
    function crash($stu_id)
    {
        if (!$stu_id){
            $result = [
                'code' => '4',
                'msg' => '学生编号不应为空',
            ];
            echo json_encode($result);
            return;

        }

        try {
            $dbh = new PDO($this->dsn, $this->user, $this->pass); //初始化一个PDO对象

            $sql = "SELECT * FROM zyh_student WHERE stu_id like '" . $stu_id . "'";
            $stmt = $dbh->query($sql, PDO::FETCH_ASSOC);
            if ($stmt->rowCount() > 0){
                $sql = "delete from zyh_cou_stu where stu_id = '" . $stu_id . "'";
                $count = $dbh->exec($sql);
                $sql = "delete from zyh_student where stu_id = '" . $stu_id . "'";
                $count += $dbh->exec($sql);
                if ($count > 0) {
                    $result = [
                        'code' => '0',
                        'msg' => '删除了' . $count . '条记录',
                    ];
                } else {
                    $result = [
                        'code' => '1',
                        'msg' => '删除了' . $count . '条记录',
                    ];
                }
            }
            else{
                $result = [
                    'code' => '2',
                    'msg' => '学生信息不存在！',
                ];

            }

            $dbh = null;

            //返回json
            echo json_encode($result);
        } catch (PDOException $e) {
            die("Error!: " . $e->getMessage() . "<br/>");
        }
    }
}
